#include "Monster.h"

Monster::Monster()
{
    _xPos = 0;
    _yPos = 0;

    _xSpeed = 0;
    _ySpeed = 0;

    _digdugX = 0;
    _digdugY = 0;

    _go_Invisible = false;

    _alive = true;

    _hole_left = false;
    _hole_right = false;
    _hole_down = false;
    _hole_up = false;
}

void Monster::initialize(int pos_x, int pos_y)//////////////////
{
    _xPos = pos_x;//////////////////
    _yPos = pos_y;/////////////////////

    _xSpeed = 1;
    _ySpeed = 1;

    _xDirection = 1;
    _yDirection = 1;

    UpDown_on = true;
    LeftRight_on = true;

}

void Monster::follow_digdugX( Man* Dig_Dug)
{
    int x = 0;

    x = Dig_Dug->get_pos_x();

    _digdugX = x;

}

void Monster::follow_digdugY( Man* Dig_Dug)
{
    int y = 0;

    y = Dig_Dug->get_pos_y();

    _digdugY = y;

}

void Monster::position_check()
{
    if( (_xPos%50==0) && (_yPos%50==0))
    {
        UpDown_on = true;
        LeftRight_on = true;
    }

    else if( (_xPos%50==0) && !(_yPos%50==0))
    {
        _xDirection = 0;
        LeftRight_on = false;
    }

    else if( !(_xPos%50==0) && (_yPos%50==0))
    {
        _yDirection = 0;
        UpDown_on = false;
    }
}

void Monster::check_soil( const vector<vector<bool> >& holes )
{
    int x = _xPos/50;
    int y = (_yPos/50) - 2;

    //ensure vector index is positive
    if( x == 0 ) x = 1;
    if( y <= 0 ) y = 1;

    //check ahead depending on direction
    bool rightStatus = holes[x+1][y];
    bool leftStatus = holes[x-1][y];
    bool upStatus = holes[x][y-1];
    bool downStatus = holes[x][y+1];

    if( leftStatus )
        _hole_left = true;
    else
        _hole_left = false;

    if( rightStatus )
        _hole_right = true;
    else
        _hole_right = false;

    if( upStatus )
        _hole_up = true;
    else
        _hole_up = false;

    if( downStatus )
        _hole_down = true;
    else
        _hole_down = false;

    if(((!_hole_left) || (!_hole_right)) && ((!_hole_down) || (!_hole_up)))
    {
        _go_Invisible = true;
    }
    else
        _go_Invisible = false;
}

void Monster::move_up()
{
    if(UpDown_on)_yPos += _ySpeed * -1;
}

void Monster::move_down()
{
    if(UpDown_on)_yPos += _ySpeed;
}

void Monster::move_left()
{
    if(LeftRight_on )
    {
        _xDirection = -1;
        _xPos += _xSpeed * -1;
    }
}

void Monster::move_right()
{
    if(LeftRight_on )
    {
        _xDirection = 1;
        _xPos += _xSpeed;
    }
}

void Monster::update()
{

    if(_alive)
    {
        int xDistance = (get_digdugX() - _xPos);
        int yDistance = (get_digdugY() - _yPos);

        if( xDistance >= 0 )
        {
           move_right();
        }
        else if( xDistance <= 0 )
        {
            move_left();
        }
        if( yDistance >= 0 )
        {
            move_down();
        }
        else if( yDistance <= 0 )
        {
            move_up();
        }

        //if monster goes out of bounds it dies
       if( _xPos < 0 || _xPos > GameWidth || _yPos > GameHeight + 50 )
       {
           _alive = false;
       }
    }

    else if (!_alive)
    {
        _xPos = 0;
        _yPos = 0;
    }
}

bool Monster::collide_arrow(int arrow_pos_x, int arrow_pos_y)
{
    if ( _xPos <= arrow_pos_x    &&
         _xPos+50 >= arrow_pos_x &&
         _yPos <= arrow_pos_y    &&
         _yPos+50 >= arrow_pos_y )
    {
        return true;
    }

    else return false;
}
